package imoocvideodev.controller;

import imoocvideodev.common.util.MD5Util;
import imoocvideodev.common.util.UUIDUtils;
import imoocvideodev.pojo.Users;
import imoocvideodev.service.UsersService;
import imoocvideodev.common.result.IMoocJSONResult;
import imoocvideodev.vo.UsersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Redi
 * @Date: 2021/1/31 11:36
 * @Description: 注册登录控制器
 */
@Api(value = "用户登录注册的接口",tags = {"注册和登录的controller"})
@RestController
public class RegisterLoginController extends BasicController  {

    @Autowired
    UsersService usersService;

    @ApiOperation(value = "用户注册",notes = "用户注册的接口")
    @PostMapping("register.do")
    public IMoocJSONResult register(@RequestBody Users users) throws Exception {
        if (StringUtils.isAnyBlank(users.getUsername(),users.getPassword())){
            return IMoocJSONResult.errorMsg("用户名或密码为空");
        }
        if (usersService.checkUserExist(users.getUsername())){
            return IMoocJSONResult.errorMsg("该用户已存在，请登录！");
        }

        users.setId(UUIDUtils.getRandomUUID());
        users.setPassword(MD5Util.MD5EncodeUtf8(users.getPassword()));
        users.setNickname(users.getUsername());
        users.setFansCounts(0);
        users.setFollowCounts(0);
        users.setReceiveLikeCounts(0);

        IMoocJSONResult result = usersService.saveUser(users);
        UsersVO usersVO = setUserRedisSessionToken(users);
        result.setData(usersVO);
        return result;
    }

    public UsersVO setUserRedisSessionToken(Users users){
        String uniqueToken = UUIDUtils.getRandomUUID();
        redisOperator.set(USER_REDIS_SESSION + ":" + users.getId() ,uniqueToken, 1000*60*30);
        UsersVO usersVO = new UsersVO();
        //复制bean值到usersVo
        BeanUtils.copyProperties(users,usersVO);
        usersVO.setUserToken(uniqueToken);
        return usersVO;
    }

    @ApiOperation(value = "用户登录", notes = "用户登录的接口")
    @PostMapping("login.do")
    public IMoocJSONResult login(@RequestBody Users users) throws Exception {
        if (StringUtils.isAnyBlank(users.getUsername(),users.getPassword())){
            return IMoocJSONResult.errorMsg("用户名或密码为空");
        }
        if (!usersService.checkUserExist(users.getUsername())){
            return IMoocJSONResult.errorMsg("该用户不存在，请注册！");
        }
        Users resultUser = usersService.login(users);
        if (resultUser == null){
            return IMoocJSONResult.errorMsg("用户名或密码不正确。");
        }
        UsersVO usersVO = setUserRedisSessionToken(resultUser);
        return IMoocJSONResult.build(200,"登录成功！",usersVO);
    }

    @ApiOperation(value = "用户登出",notes = "用户登出的接口")
    @ApiImplicitParam(name = "userId",value = "用户ID",required = true,dataType = "String", paramType = "query")
    @PostMapping("logout.do")
    public IMoocJSONResult logout(String userId) {
        redisOperator.del(USER_REDIS_SESSION + ":" + userId);
        return IMoocJSONResult.okMsg("用户登出成功");
    }


}
